package itsm.isperp.module.service.bpm;

import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.BpmProcessSetting;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.repository.bpm.BpmProcessSettingMapper;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2013-6-13
 * 
 */
@Service
public class BpmProcessSettingService extends BaseService<BpmProcessSetting> {

	@Resource
	public BpmProcessSettingMapper bpmProcessSettingMapper;

	@Resource
	public JdbcTemplate jdbcTemplate;

	@Override
	public BpmProcessSettingMapper getRepository() {
		return bpmProcessSettingMapper;
	}

	public List<DbEntity> findProcessDbEntityList() {
		return this.getRepository().findProcessDbEntityList();
	}

	public List<Map<String, Object>> findProcessRelationList() {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("use_for_relation", JqGridFilterRuleOp.eq, "1");

		return this.getRepository().findAllDetailByRequest(req);
	}

	public List<Map<String, Object>> findProcessRelationList(String processKey) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("use_for_relation", JqGridFilterRuleOp.eq, "1");
		req.addFilterRule("act_re_procdef_key", JqGridFilterRuleOp.eq, processKey);

		return this.getRepository().findAllDetailByRequest(req);
	}

	/**
	 * 
	 * @return
	 */
	public List<DbEntity> findProcessDbForSlmEntityList() {
		return this.getRepository().findProcessDbForSlmEntityList();
	}

	/**
	 * 根据流程定义的key获取实体
	 * 
	 * @param processKey
	 *            流程定义的key
	 * @return 数据实体
	 */
	public DbEntity findDbEntityByKey(String processKey) {

		return this.getRepository().findDbEntityByKey(processKey);
	}

	public String findDbEntityIdByKey(String processKey) {
		return this.getRepository().findDbEntityIdByKey(processKey);
	}

	public void updateEntityTaskId(DbEntity entity, String id, String taskId) {
		this.updateEntityTaskId(entity.getTableName(), id, taskId);
	}

	public void updateEntityTaskId(String tableName, String id, String processId) {
		String sql = "UPDATE " + tableName + " SET process_id='" + processId + "' WHERE id='" + id + "'";

		this.jdbcTemplate.update(sql);
	}

	public String getStartFormByProcDefKey(String processKey) {
		String sql = "select * from bpm_process_setting where act_re_procdef_key='" + processKey + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if (list.size() > 0) {
			return list.get(0).get("form_start_url").toString();
		} else {
			return "";
		}
	}

	/**
	 * 根据流程定义的key获取到所关联实体的尸体字段名及数据库列名
	 * 
	 * @param processKey
	 *            流程定义的key
	 * @return 关联实体的尸体字段名及数据库列名
	 */
	public List<Map<String, Object>> findDbEntityColumnByProcDefKey(String processKey) {
		return this.getRepository().findDbEntityColumnByProcDefKey(processKey);
	}

}
